package com.example.xyblog.Controller;



import com.example.xyblog.Common.AjaxRespons;
import com.example.xyblog.Common.ConstantVariable;
import com.example.xyblog.Model.Articleinfo;
import com.example.xyblog.Model.Userinfo;
import com.example.xyblog.Service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 获取当前登陆用户的文章
     * @param request
     * @return
     */
    @RequestMapping("/getMyList")
    public Object getMyList( HttpServletRequest request){
        HttpSession session= request.getSession(false);
        System.out.println(1);
        if(session != null && session.getAttribute(ConstantVariable.SESSION_KEY_VALUE) != null){
            Userinfo userInfo= (Userinfo) session.getAttribute(ConstantVariable.SESSION_KEY_VALUE);
            //从数据库中拿到文章存在List里面
            List<Articleinfo> list=articleService.getMyList(userInfo.getId());
            return list;
        }
        return null;

    }


    /**
     * 文章详情
     * @param id
     * @return
     */
    @RequestMapping("/getBlog")
    public Object getBlog(Integer id){
        if(id == null){
            return AjaxRespons.fail(-1,"传入数据出错");
        }
        Articleinfo articleinfo=articleService.getBlog(id);
        if(articleinfo == null){
            return AjaxRespons.fail(-1,"暂无查询数据");
        }
        return articleinfo;
    }

    @RequestMapping("/getAll")
    public Object getAll(){
        List<Articleinfo> list=articleService.geAll();
        if(list == null){
            return AjaxRespons.fail(-1,"暂无数据");
        }
        return list;
    }

    @RequestMapping("/getBlogById")
    public Object getBlogById(Integer id){
        if(id == null){
            return AjaxRespons.fail(-1,"id出错");
        }
        Articleinfo articleinfo=articleService.getBlogById(id);
        if (articleinfo == null){
            return AjaxRespons.fail(-1,"暂未查询到文章");
        }
        return articleinfo;
    }

    @RequestMapping("/update")
    public Object update(String title,String content,Integer aid){
        //非空校验
        if(StringUtils.hasLength(title) && StringUtils.hasLength(content) &&
                aid != null && aid > 0){
            //有标题有正文,接下来操作数据库
            int ret=articleService.update(title,content,aid,1);
            return ret;
        }else {
            return -1;
        }

    }

    @RequestMapping("/thumb")
    //todo:一个人一篇文章只能点赞一次，再次点赞为取消点赞
    public Object thumb(Integer aid){
        System.out.println(aid);
        if(aid == null){
            return AjaxRespons.fail(-1,"id出错");
        }
        Articleinfo articleinfo=articleService.getBlog(aid);
        Integer tnum=articleinfo.getThumb();
        tnum++;
        articleService.updateThumb(tnum,aid);
        articleinfo=articleService.getBlog(aid);
        return articleinfo;
    }

    /**
     * 发布文章
     */
    @RequestMapping("/edit")
    public Object editArt(String title,String content,HttpServletRequest request){
        if(StringUtils.hasLength(title) && StringUtils.hasLength(content)){
            //有标题有正文,接下来操作数据库
            HttpSession session=request.getSession(false);
            Userinfo userinfo=(Userinfo) session.getAttribute(ConstantVariable.SESSION_KEY_VALUE);
            int ret=articleService.editArt(title,content,userinfo.getId(),1);
            return ret;
        }else {
            return -1;
        }

    }

    @RequestMapping("/draftSave")
    public Object draftSave(String title,String content,HttpServletRequest request){
        if(StringUtils.hasLength(title) && StringUtils.hasLength(content)){
            //有标题有正文,接下来操作数据库
            HttpSession session=request.getSession(false);
            Userinfo userinfo=(Userinfo) session.getAttribute(ConstantVariable.SESSION_KEY_VALUE);
            int ret=articleService.editArt(title,content,userinfo.getId(),0);
            return ret;
        }else {
            return -1;
        }
    }

    /**
     * 进行分页
     */
    @RequestMapping("/getCut")
    public Object getCut(Integer psize,Integer pindex){
        if(psize <= 0 || pindex <=0){
            return AjaxRespons.fail(401,-1,"传入数据出错");
        }
        return articleService.getCut(psize,pindex);
    }

    @RequestMapping("/getTotal")
    public Object getTotal(Integer psize){
        if(psize <= 0){
            return AjaxRespons.fail(401,-1,"传入数据出错");
        }
        int totalPage=(articleService.geAll().size()/psize)+1;
        return totalPage;
    }

}





